package com.ocm.commerceinteraction.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonSyntaxException;
import com.ocm.commerceinteraction.GlobalApplication;
import com.ocm.commerceinteraction.R;
import com.ocm.commerceinteraction.config.RequestAddr;
import com.ocm.commerceinteraction.model.BaseModel;
import com.ocm.commerceinteraction.model.SearchUserList;
import com.ocm.commerceinteraction.util.http.AjaxCallBack;
import com.ocm.commerceinteraction.util.loading.Loading;
import com.ocm.commerceinteraction.util.view.MyAlertDialog;
import com.squareup.picasso.Picasso;

/**
 * 课程计划
 *
 * @author zqy
 */
public class SearchUserListAdapter extends BaseAdapter {

    private Context mContext;
    LayoutInflater inflater;
    SearchUserList searchUserList;

    public SearchUserListAdapter(Context context, SearchUserList searchUserList) {
        mContext = context;
        inflater = LayoutInflater.from(context);
        this.searchUserList = searchUserList;
    }


    public final class ViewHolder {
        public ViewHolder() {
            // TODO Auto-generated constructor stub
        }

        public ImageView headImage; // 头像
        public TextView userName;// 用户名
        public TextView addBtn;// 添加按钮
    }

    @SuppressWarnings("null")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.search_list_item, null);
            holder.headImage = (ImageView) convertView.findViewById(R.id.head);
            holder.userName = (TextView) convertView.findViewById(R.id.title);
            holder.addBtn = (TextView) convertView.findViewById(R.id.tv_add_user_btn);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.userName.setText(searchUserList.getData().getList().get(position).getNick());
        Picasso.with(mContext).load(RequestAddr.baseUrl + searchUserList.getData().getList().get(position).getIcon()).into(holder.headImage);

        holder.addBtn.setTag(position);
        holder.addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int posi = (int) v.getTag();
                showEditDiallog(posi);

            }
        });

        return convertView;
    }

    MyAlertDialog joinGroupDialog;

    void showEditDiallog(final int position) {
        joinGroupDialog = new MyAlertDialog(mContext);
        joinGroupDialog.setTitle("入群申请");
        joinGroupDialog.setMessage("");
        joinGroupDialog.setNegativeButton("   确定    ", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                joinGroupDialog.dismiss();
                addUser(position, joinGroupDialog.getMessage());
            }
        });

        joinGroupDialog.setPositiveButton("", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                joinGroupDialog.dismiss();
            }
        });
    }

    void addUser(int position, String keywords) {
        String requestUrl = RequestAddr.addUser +
                "?friend_uniq_id=" + searchUserList.getData().getList().get(position).getUser_uniq_id()+
                "&keywords="+keywords;
        GlobalApplication.xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                Loading.stopLoading();
                dealBaseData(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                Loading.stopLoading();
            }
        });
    }

    /**
     * 处理返回基本结构信息数据
     *
     * @param object  返回json结构
     */
    BaseModel baseModel;

    protected void dealBaseData(Object object) {
        try {
            baseModel = GlobalApplication.gson.fromJson(object.toString(), BaseModel.class);
        } catch (JsonSyntaxException e) {
            baseModel = GlobalApplication.gson.fromJson(object.toString(), BaseModel.class);
        }
        Toast.makeText(mContext, baseModel.getMsg(), Toast.LENGTH_SHORT).show();
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return searchUserList.getData().getList().size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
}
